package com.laiketui.common.process;

import com.laiketui.common.api.order.OrderDubboService;
import com.laiketui.core.exception.LaiKeAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.laiketui.domain.lktconst.ErrorCode.BizErrorCode.PARAMATER_ERROR;


@Component
public class HandlerOrderContext {

    @Autowired
    private ApplicationContext applicationContext;

    //存放所有策略类Bean的map
    public static Map<String, Class<OrderDubboService>> orderStrategyBeanMap= new HashMap<>();

    public OrderDubboService getOrderService(String orderType) throws LaiKeAPIException {
        Class<OrderDubboService> strategyClass = orderStrategyBeanMap.get(orderType);
        if(strategyClass==null) {
            throw new LaiKeAPIException(PARAMATER_ERROR, "参数错误：不支持的订单类型！");
        }
        //从容器中获取对应的策略Bean
        return applicationContext.getBean(strategyClass);
    }

}
